home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 6.5 KB | 216 lines | [TEXT/KAHL] |
- /********************************************************* DEFINITION
- DATE: 9/17/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPWriteTask
-
- SUPERCLASS: CPPPeriodicTask
-
- This C++ class lets you write data asynchronously through a
- PPC connection.
-
- ********************************************************************/
-
- #include <CPPWriteTask.h>
- #include <CPPTaskManager.h>
- #include <MemoryTools.h>
- #include <CPPNodeInfo.h>
- #include <ToolboxTools.h>
- #include <StringTools.h>
- #include <script.h>
-
- /*-----------------------------------------------------------------*/
- /*----------------------- INTERNAL CONSTANTS ----------------------*/
- /*-----------------------------------------------------------------*/
-
- #define kSync FALSE
- #define kAsync TRUE
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPWriteTask::CPPWriteTask (CPPTaskManager *TaskManager,
- long minPeriod,
- Boolean deleteWhenDone) :
- CPPPeriodicTask (TaskManager, minPeriod,
- deleteWhenDone)
- {
- this->data = NULL;
- this->ownsData = FALSE;
- this->dataLen = 0;
- this->sessionID = 0;
- this->writePBPtr = NULL;
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPWriteTask::~CPPWriteTask (void)
- {
- PPCClosePBRec CloseRec;
-
- // if there is a write call outstanding, close the session
- if (this->writePBPtr)
- if (this->writePBPtr->ioResult == 1)
- {
- CloseRec.portRefNum = this->sessionID;
- PPCClose(&CloseRec, FALSE);
- }
-
- // dispose of the data and control pointers
- NukePtr(this->writePBPtr);
- if (this->ownsData)
- NukePtr(this->data);
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPWriteTask::ClassName (void)
- {
- return "CPPWriteTask";
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPWriteTask::StartWriteTask (PPCPortRefNum SourcePortRefNum,
- CPPNodeInfo *SendTo,
- Ptr DataToWrite, Boolean OwnsData,
- CompletionProc DoProc,
- OSType DataType, OSType DataCreator)
- /* Open a connection between SourcePort and SendTo, then */
- /* set up the write parameter block and make the write call */
- {
- EntityName TheName;
- StringPtr ObjStr = NULL, TypeStr = NULL, ZoneStr = NULL;
- LocationNameRec *LocationRec = (LocationNameRec *)NewPtrClear(sizeof(LocationNameRec));
- PPCStartPBRec StartRec;
- OSErr ErrCode;
- PortInfoRec *PortRec = (PortInfoRec *)NewPtrClear(sizeof(PortInfoRec));
- StringPtr PName = PortRec->name.name;
- short i = 1;
-
- if (!this->hasCompleted)
- return;
-
- // fill in the Location Rec with data from the SendTo object;
- SendTo->GetNodeName (&ObjStr, &TypeStr, &ZoneStr);
- CopyString (ObjStr, TheName.objStr);
- CopyString (TypeStr, TheName.typeStr);
- CopyString (ZoneStr, TheName.zoneStr);
- LocationRec->locationKindSelector = ppcNBPLocation;
- LocationRec->u.nbpEntity = TheName;
-
- // fill in the PPCPortRec with the destination port name
- PortRec->name.nameScript = smRoman;
-
- CopyString(TypeStr, PName);
- while (((PName[i] & 0x00FF) != 0xA5) && (i <= PName[0]))
- i++;
- PName[0] = i-1;
- PortRec->name.portKindSelector = ppcByString;
- CopyString ("\pPPCCommPort", PortRec->name.u.portTypeStr);
-
- // fill in the 'start session' record
- StartRec.ioCompletion = NULL;
- StartRec.portRefNum = SourcePortRefNum;
- StartRec.serviceType = ppcServiceRealTime;
- StartRec.resFlag = 0;
- StartRec.portName = &PortRec->name;
- StartRec.locationName = LocationRec;
- StartRec.userData = 0;
- StartRec.userRefNum = 0; // guest access
-
- // Try to open the connection; if we can, create a WriteTask
- // to send the data to the other machine asynchronously
- if ((ErrCode = PPCStart (&StartRec, FALSE)) != noErr)
- ErrorAlert (ErrCode, "\pCould not send the message");
- else
- // call the other StartWriteTask method to finish the job
- this->StartWriteTask(DataToWrite, ownsData,
- StartRec.sessRefNum, DoProc,
- DataType, DataCreator);
-
- // dispose of the memory used by the PPCStart routine
- NukePtr(PortRec);
- NukePtr(LocationRec);
- }
-
-
- /*-----------------------------------------------------------------*/
-
- void CPPWriteTask::StartWriteTask (Ptr DataToWrite, Boolean OwnsData,
- PPCSessRefNum ConnectionID,
- CompletionProc DoProc,
- OSType DataType, OSType DataCreator)
- /* Set up the write parameter block and make the call, assuming */
- /* that a connection is already open and its refnum is passed in */
- /* ConnectionID */
- {
- this->SetCompletionProc(DoProc);
-
- if (!this->hasCompleted)
- return;
-
- this->writePBPtr = (PPCWritePBPtr)NewPtrClear(sizeof(PPCWritePBRec));
- if ((this->callResult = MemError()) == noErr)
- {
- // initialize the data tracking variables
- this->data = DataToWrite;
- this->ownsData = OwnsData;
- this->dataLen = GetPtrSize(DataToWrite);
- this->sessionID = ConnectionID;
-
- // set up the WritePB's fields
- this->writePBPtr->ioCompletion = NULL;
- this->writePBPtr->sessRefNum = ConnectionID;
- this->writePBPtr->bufferLength = this->dataLen;
- this->writePBPtr->bufferPtr = DataToWrite;
- this->writePBPtr->more = FALSE;
- this->writePBPtr->userData = this->dataLen;
- this->writePBPtr->blockCreator = DataCreator;
- this->writePBPtr->blockType = DataType;
-
- // make the call to write the data asynchronously
- if ((this->callResult = PPCWrite(this->writePBPtr, kAsync)) == noErr)
- {
- this->hasCompleted = FALSE; // note that we are not done
- this->ourManager->AddPeriodicTask(this); // now add the task
- return;
- }
- }
-
- // we will reach this line if memory allocation or PPCWrite fails
- NukePtr (this->writePBPtr);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPWriteTask::DoPeriodicAction (void)
- /* if the write has completed or failed, store the result code */
- /* and note that we are done */
- {
- // call the inherited method to update frequency count
- CPPPeriodicTask::DoPeriodicAction();
-
- if (this->writePBPtr->ioResult != 1)
- {
- this->callResult = this->writePBPtr->ioResult;
- this->hasCompleted = TRUE;
-
- if (this->callResult != noErr)
- SysBeep(1);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPWriteTask::DoCompletedAction(void)
- {
- // release the memory we allocated
- NukePtr(this->writePBPtr);
- if (this->ownsData)
- NukePtr(this->data);
-
- CPPPeriodicTask::DoCompletedAction();
- }
-